define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'building/index' + location.search,
                    add_url: 'building/add',
                    edit_url: 'building/edit',
                    del_url: 'building/del',
                    multi_url: 'building/multi',
                    company_url: 'building/company',
                    delcompany_url: 'building/delcompany',
                    table: 'building',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('建筑名称'), operate: 'LIKE' },
                        { field: 'prov', title: __('省份'), operate: 'LIKE' },
                        { field: 'city', title: __('城市'), operate: 'LIKE' },
                        { field: 'region', title: __('区域'), operate: 'LIKE', visible: false },
                        { field: 'street', title: __('街道'), operate: 'LIKE', visible: false },
                        { field: 'address', title: __('详细地址'), operate: 'LIKE' },
                        { field: 'lat', title: __('纬度'), visible: false },
                        { field: 'lng', title: __('经度'), visible: false },
                        { field: 'createtime', title: __('创建时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('更新时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'status', title: __('状态'), searchList: { "normal": __('Normal'), "hidden": __('Hidden') }, formatter: Table.api.formatter.status },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'company',
                                text: __('关联公司'),
                                title: __('关联公司'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-link',
                                url: 'building/company'
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        company: function () {
            Controller.api.bindevent();
            // 绑定删除按钮事件
            $(document).on('click', '.btn-delone', function () {
                var id = $(this).data('id');
                var that = this;
                Layer.confirm(
                    __('Are you sure you want to delete this item?'),
                    { icon: 3, title: __('Warning'), shadeClose: true },
                    function (index) {
                        Fast.api.ajax({
                            url: "building/delcompany",
                            data: { ids: id }
                        }, function (data, ret) {
                            $(that).closest('tr').remove();
                            Layer.close(index);
                            return false;
                        });
                    }
                );
            });

            // 表单提交成功后刷新列表
            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                Fast.api.close(data);
                Toastr.success("成功");
                // 刷新页面
                location.reload();
            }, function (data, ret) {
                Toastr.error(ret.msg);
                return false;
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
}); 